home *** CD-ROM | disk | FTP | other *** search
- /*______________________________________________________________________
-
- misc.c - Miscellany Module
-
- Copyright © 1988, 1989, 1990 Northwestern University. Permission is
- granted to use this code in your own projects, provided you give credit
- to both John Norstad and Northwestern University in your about box or
- document.
-
- This module takes care of various miscellaneous tasks.
- _____________________________________________________________________*/
-
-
- #pragma load "precompile"
- #include "utl.h"
- #include "scn.h"
- #include "vol.h"
- #include "rep.h"
- #include "rez.h"
- #include "glob.h"
- #include "vmsg.h"
- #include "wstm.h"
- #include "unmount.h"
- #include "misc.h"
- #include "prog.h"
- #include "main.h"
- #include "vscn.h"
-
- #pragma segment misc
-
- /*______________________________________________________________________
-
- misc_HiliteScan - Hilite Scan and Disinfect Buttons.
-
- Exit: Scan and Disinfect buttons hilited iff there is a current
- volume.
- _____________________________________________________________________*/
-
-
- void misc_HiliteScan(void)
-
- {
- short vRefNum; /* ref num of selected vol */
- short hiliteVal; /* 0 to hilite, 255 to unhilite */
-
- main_SetPort();
- hiliteVal = (vol_GetSel(&vRefNum) == nsvErr) ? 255 : 0;
- HiliteControl(Controls[scanID-firstControl], hiliteVal);
- HiliteControl(Controls[disinfectID-firstControl],
- DisableDisinfect ? 255 : hiliteVal);
- }
-
- /*______________________________________________________________________
-
- misc_ClearReport - Clear the Report.
- _____________________________________________________________________*/
-
-
- void misc_ClearReport (void)
-
- {
- Rect inval; /* rect to be invalidated */
-
- rep_Clear(Report);
- rep_Fill(Report, repHeadID, false);
- rep_GetRect(Report, &inval);
- InsetRect(&inval, 1, 1);
- InvalRect(&inval);
- RepInfected = false;
- vmsg_ClearTags();
- };
-
- /*______________________________________________________________________
-
- LoadAll - Load all Disinfectant Resources of a Given Type
-
- Entry: rType = resource type.
- _____________________________________________________________________*/
-
-
- static void LoadAll (OSType rType)
-
- {
- short i; /* loop index */
- short n; /* number of resources of type */
- Handle h; /* handle to resource */
-
- n = CountResources(rType);
- SetResLoad(false);
- for (i = 1; i <= n; i++) {
- h = GetIndResource(rType, i);
- if (!*h && HomeResFile(h) == DfectRefNum) LoadResource(h);
- };
- SetResLoad(true);
- }
-
- /*______________________________________________________________________
-
- misc_CheckEject - Check for Eject of System or Disinfectant Volume
-
- Entry: refNum = vol ref num or drive num of volume about
- to be ejected.
-
- If the system volume or Disinfectant's volume is about to be ejected,
- this routine loads all the resources which might be needed from the system
- file or from the Disinfectant file.
- _____________________________________________________________________*/
-
-
- void misc_CheckEject (short refNum)
-
- {
- Handle h; /* handle to resource */
- short vRefNum; /* vol ref num */
- Str255 volName; /* vol name */
- long freeBytes; /* free bytes on vol */
- Handle hdlog; /* handle to please wait dialog template */
- DialogPtr dlog; /* pointer to please wait dialog */
- static Boolean firstTime=true; /* true if haven't preloaded
- resources yet */
-
- /* Convert a drive number to a vol ref number. */
-
- if (refNum > 0) {
- (void) GetVInfo(refNum, volName, &vRefNum, &freeBytes);
- } else {
- vRefNum = refNum;
- };
-
- /* Put up "Please wait" dialog if we need to load resources for
- the first time. */
-
- dlog = nil;
- if (vRefNum == SysVol || vRefNum == DfectVol) {
- if (firstTime) {
- SetCursor(*Watch);
- hdlog = GetResource('DLOG', plsWaitID);
- HNoPurge(hdlog);
- utl_CenterDlogRect(*(Rect**)hdlog, false);
- dlog = GetNewDialog(plsWaitID, nil, (WindowPtr)-1);
- ShowWindow((WindowPtr)dlog);
- DrawDialog(dlog);
- };
- firstTime = false;
- } else {
- return;
- };
-
- /* If ejecting Disinfectant volume, load Disinfectant resources that
- we might need later. */
-
- if (vRefNum == DfectVol) {
- SetCursor(*Watch);
- LoadAll('PICT');
- LoadAll('WIND');
- LoadAll('CNTL');
- LoadAll('ALRT');
- LoadAll('DITL');
- LoadAll('DLOG');
- LoadAll('STR#');
- LoadAll('SICN');
- LoadAll('CURS');
- LoadAll('TCON');
- LoadAll('TAG ');
- LoadAll('CELL');
- LoadAll('LDEF');
- LoadAll('CODE');
- LoadAll('CREA');
- };
-
- /* If ejecting System volume, load system resources that we
- might need later. */
-
- if (vRefNum == SysVol) {
- SetCursor(*Watch);
- (void) utl_CheckPack(listMgr, true);
- (void) utl_CheckPack(stdFile, true);
- (void) utl_CheckPack(dskInit, true);
- (void) utl_CheckPack(intUtil, true);
- (void) utl_CheckPack(bdConv, true);
- CouldDialog(putDlgID);
- CouldAlert(sfNoPrivs);
- CouldAlert(sfBadChar);
- CouldAlert(sfSysErr);
- CouldAlert(sfReplaceExisting);
- CouldAlert(sfDiskLocked);
- utl_DILoad();
- h = GetResource('MBDF', 0);
- h = GetResource('MDEF', 0);
- h = GetResource('itl0', 0);
- h = GetResource('ICON', stopIcon);
- h = GetResource('ICON', noteIcon);
- /* call SysBeep to force snd and snth resources to be loaded. */
- SysBeep(beepDuration);
- };
-
- /* Dispose of please wait dialog. */
-
- if (dlog) {
- DisposDialog(dlog);
- HPurge(hdlog);
- };
-
- misc_SetCursor();
- }
-
- /*______________________________________________________________________
-
- misc_SetCursor - Set Cursor.
- _____________________________________________________________________*/
-
-
- void misc_SetCursor (void)
-
- {
- if (!InForeground) return;
- if (HelpMode) {
- SetCursor(*HelpCurs);
- misc_DisableBB();
- } else if (!Scanning || misc_GetWindKind(FrontWindow()) != mainWind) {
- InitCursor();
- misc_DisableBB();
- } else if (FloppyWait) {
- SetCursor(*FloppyCurs);
- misc_DisableBB();
- } else if (Scanning) {
- utl_InitSpinCursor(BBCursors, numBalls, spinInterval);
- misc_EnableBB();
- };
- }
-
- /*______________________________________________________________________
-
- misc_ValPrint - Validate Print Record.
-
- Entry: p = pointer to rpp param block.
- p->hPrint = pointer to print record, or nil if none
- yet allocated.
- report = true if this is for a report printing operation.
- report = false if this is for a document printing operation.
-
- Exit: p->hPrint = pointer to print record. A new one is allocated
- if necessary, and any existing one is validated.
-
- If a new print record is allocated, or if the existing one
- was invalid, then the other fields in the rpp param block
- are also initialized. The initial values depend on whether
- this is a report or document being printed, and whether
- the output device is a LaserWriter or ImageWriter.
- _____________________________________________________________________*/
-
-
- void misc_ValPrint (rpp_PrtBlock *p, Boolean report)
-
- {
- Boolean laser; /* true if laserwriter */
- char *defFont1; /* pointer to first default font name */
- char *defFont2; /* pointer to second default font name */
- short defSize1; /* first default size */
- short defSize2; /* second default size */
-
- if (p->hPrint) {
- if (!PrValidate(p->hPrint)) return;
- } else {
- p->hPrint = (THPrint)NewHandle(sizeof(TPrint));
- PrintDefault(p->hPrint);
- defSize1 = (**(p->hPrint)).prInfo.iVRes;
- };
- laser = utl_IsLaser(p->hPrint);
- if (report) {
- if (laser) {
- defFont1 = defFont2 = "\pHelvetica";
- } else {
- defFont1 = defFont2 = "\pGeneva";
- };
- defSize1 = defSize2 = 10;
- p->topMargin = p->botMargin = 75;
- p->leftMargin = p->rightMargin = 150;
- } else {
- if (laser) {
- defFont1 = "\pPalatino";
- defSize1 = 10;
- defFont2 = "\pTimes";
- defSize2 = 12;
- } else {
- defFont1 = defFont2 = "\pGeneva";
- defSize1 = defSize2 = 10;
- };
- p->topMargin = p->botMargin = 50;
- p->leftMargin = p->rightMargin = 100;
- };
- p->reverseOrder = laser ? true : false;
- if (utl_GetFontNumber(defFont1, &p->fontNum)) {
- p->fontSize = defSize1;
- } else if (utl_GetFontNumber(defFont2, &p->fontNum)) {
- p->fontSize = defSize2;
- } else {
- p->fontNum = applFont;
- p->fontSize = defSize1;
- };
- p->header = true;
- p->titleSep = report ? 25 : 40;
- p->titleFont = p->fontNum;
- p->titleStyle = bold;
- p->titleSize = utl_ScaleFontSize(p->fontNum, p->fontSize, 90, laser);
- p->dlogID = prDlogID;
- p->tabConID = tconID;
- p->emptyPageRangeID = noPagesID;
- p->ditlID = pageSetupID;
- p->sizeRangeID = illegalSizeID;
- p->marginsTooBigID = marTooBigID;
- p->minFontSize = 1;
- p->maxFontSize = 24;
- p->updateAll = misc_Update;
- }
-
- /*______________________________________________________________________
-
- misc_ReadPref - Read the Preferences File.
- _____________________________________________________________________*/
-
-
- #pragma segment init
-
- void misc_ReadPref (void)
-
- {
- ParamBlockRec pBlockMFS; /* param block for PBSetVol call */
- WDPBRec pBlockHFS; /* param block for PBHSetVol call */
- Str255 fName; /* "Disinfectant Prefs" file name */
- OSErr rCode; /* result code */
- short refNum; /* resource file refnum */
- Boolean error; /* true if error encountered */
- char version[30]; /* version number */
- Handle thePrefs; /* handle to prefs resource */
-
- /* Set the default volume and directory to the system volume and the
- blessed folder. */
-
- if (utl_VolIsMFS(SysVol)) {
- pBlockMFS.volumeParam.ioNamePtr = nil;
- pBlockMFS.volumeParam.ioVRefNum = SysVol;
- rCode = PBSetVol(&pBlockMFS, false);
- } else {
- pBlockHFS.ioNamePtr = nil;
- pBlockHFS.ioVRefNum = SysVol;
- pBlockHFS.ioWDDirID = SysDirID;
- rCode = PBHSetVol((WDPBPtr)&pBlockHFS, false);
- };
- error = rCode;
-
- /* Open the "Disinfectant Prefs" resource file with read-only permission. */
-
- refNum = -1;
- if (!error) {
- GetIndString(fName, strListID, prefsFileName);
- if (OldRom) {
- refNum = OpenResFile(fName);
- } else {
- refNum = OpenRFPerm(fName, 0, fsRdPerm);
- };
- error = refNum == -1;
- };
-
- /* Load the prefs resource. */
-
- if (!error) {
- thePrefs = GetResource('DPRF', 0);
- error = !thePrefs;
- };
-
- /* Check for the proper size of the resource. */
-
- if (!error) error = GetHandleSize(thePrefs) != sizeof(Prefs);
-
- /* Copy the contents of the prefs resource to the global Prefs variable. */
-
- if (!error) memcpy(&Prefs, *thePrefs, sizeof(Prefs));
-
- /* Check the version number. */
-
- if (!error) {
- GetIndString(version, strListID, versionStr);
- error = !EqualString(version, Prefs.version, false, false);
- };
-
- /* Copy the print records into relocatable blocks, and store handles
- to them in the print blocks. */
-
- if (!error && Prefs.mainPrint.hPrint)
- error = PtrToHand((Ptr)&Prefs.mainPrintRec,
- (Handle*)&Prefs.mainPrint.hPrint,
- sizeof(TPrint));
- if (!error && Prefs.helpPrint.hPrint)
- error = PtrToHand((Ptr)&Prefs.helpPrintRec,
- (Handle*)&Prefs.helpPrint.hPrint,
- sizeof(TPrint));
-
- /* If any error was encountered, initialize the Prefs global variable to
- default values. */
-
- if (error) {
- GetIndString(Prefs.version, strListID, versionStr);
- wstm_Init(&Prefs.mainState);
- wstm_Init(&Prefs.helpState);
- wstm_Init(&Prefs.prefState);
- wstm_Init(&Prefs.abouState);
- Prefs.mainPrint.hPrint = nil;
- Prefs.helpPrint.hPrint = nil;
- Prefs.numOpenWind = 1;
- Prefs.openWind[0] = mainWind;
- Prefs.helpScrollPos = 0;
- Prefs.beepCount = 0;
- Prefs.scanningStation = false;
- Prefs.scanningStationOp = checkOp;
- Prefs.repCreator = 'ttxt';
- Prefs.docCreator = 'MSWD';
- Prefs.repOtherCre = '????';
- Prefs.docOtherCre = '????';
- Prefs.notifOption = notifIcon;
- };
-
- /* Close the resource file. */
-
- if (refNum != -1) CloseResFile(refNum);
- }
-
- #pragma segment misc
-
- /*______________________________________________________________________
-
- misc_WritePref - Write the Preferences File.
- _____________________________________________________________________*/
-
-
- void misc_WritePref (void)
-
- {
- ParamBlockRec pBlockMFS; /* param block for PBSetVol call */
- WDPBRec pBlockHFS; /* param block for PBHSetVol call */
- Str255 fName; /* "Disinfectant Prefs" file name */
- OSErr rCode; /* result code */
- short refNum; /* resource file refnum */
- Boolean error; /* true if error encountered */
- FInfo fndrInfo; /* Finder info */
- Handle thePrefs; /* handle to prefs resource */
- short numOpenWind; /* number of open windows */
- WindowPtr curWind; /* ptr to cur window */
- WindowObject *curObj; /* ptr to cur window object */
- WindowPtr nextWind; /* ptr to next window */
- WindKind windKind; /* window kind */
-
- /* Walk the window list. Close all windows, and save the open window
- states and orders in the Prefs variable. */
-
- numOpenWind = 0;
- curWind = FrontWindow();
- while (curWind) {
- nextWind = (WindowPtr)((WindowPeek)curWind)->nextWindow;
- windKind = misc_GetWindKind(curWind);
- if (windKind != daWind) {
- Prefs.openWind[numOpenWind] = windKind;
- numOpenWind++;
- curObj = (WindowObject*)((WindowPeek)curWind)->refCon;
- (*curObj->close)();
- };
- curWind = nextWind;
- };
- Prefs.numOpenWind = numOpenWind;
-
- /* Copy the print records into the Prefs variable. */
-
- if (Prefs.mainPrint.hPrint)
- memcpy(&Prefs.mainPrintRec, *Prefs.mainPrint.hPrint, sizeof(TPrint));
- if (Prefs.helpPrint.hPrint)
- memcpy(&Prefs.helpPrintRec, *Prefs.helpPrint.hPrint, sizeof(TPrint));
-
- /* Set the default volume and directory to the system volume and the
- blessed folder. */
-
- if (utl_VolIsMFS(SysVol)) {
- pBlockMFS.volumeParam.ioNamePtr = nil;
- pBlockMFS.volumeParam.ioVRefNum = SysVol;
- rCode = PBSetVol(&pBlockMFS, false);
- } else {
- pBlockHFS.ioNamePtr = nil;
- pBlockHFS.ioVRefNum = SysVol;
- pBlockHFS.ioWDDirID = SysDirID;
- rCode = PBHSetVol((WDPBPtr)&pBlockHFS, false);
- };
- error = rCode;
-
- /* Open the "Sample Prefs" resource file with read/write permission.
- If it doesn't exist, create it with type SMP1 and creator SAMP. */
-
- refNum = -1;
- if (!error) {
- GetIndString(fName, strListID, prefsFileName);
- if (OldRom) {
- refNum = OpenResFile(fName);
- } else {
- refNum = OpenRFPerm(fName, 0, fsRdWrPerm);
- };
- if (refNum == -1) {
- CreateResFile(fName);
- if (!(error = ResError())) {
- if (!(error = GetFInfo(fName, 0, &fndrInfo))) {
- fndrInfo.fdType = 'D2C1';
- fndrInfo.fdCreator = 'D2CT';
- if (!(error = SetFInfo(fName, 0, &fndrInfo))) {
- if (OldRom) {
- refNum = OpenResFile(fName);
- } else {
- refNum = OpenRFPerm(fName, 0, fsRdWrPerm);
- };
- error = refNum == -1;
- };
- };
- };
- };
- };
-
- /* Load the prefs resource. If no such resource exists, create one. */
-
- if (!error) {
- thePrefs = GetResource('DPRF', 0);
- if (!thePrefs) {
- thePrefs = NewHandle(sizeof(Prefs));
- AddResource(thePrefs, 'DPRF', 0, "\p");
- error = ResError();
- };
- };
-
- /* Set the proper size of the resource and copy the Prefs global variable
- to the resource. */
-
- if (!error) error = PtrToXHand((Ptr)&Prefs, thePrefs, sizeof(Prefs));
-
- /* Mark the resource changed. */
-
- if (!error) ChangedResource(thePrefs);
-
- /* Close the resource file. */
-
- if (refNum != -1) CloseResFile(refNum);
- }
-
- /*______________________________________________________________________
-
- misc_GetWindKind - Get Window Kind.
-
- Entry: theWindow = pointer to window.
-
- Exit: function result = window kind.
- _____________________________________________________________________*/
-
-
- WindKind misc_GetWindKind (WindowPtr theWindow)
-
- {
- return utl_IsDAWindow(theWindow) ?
- daWind :
- ((WindowObject*)((WindowPeek)theWindow)->refCon)->windKind;
- }
-
- /*______________________________________________________________________
-
- misc_Notify - Notify User.
-
- Entry: msg = index in STR# resource of message.
- wait = true to wait until brought to foreground.
- _____________________________________________________________________*/
-
-
- void misc_Notify (short msg, Boolean wait)
-
- {
- if (InForeground) return;
- if (utl_SysHasNotMgr && !Notified) {
- Notified = true;
- NotifRec.qType = nmType;
- NotifRec.nmMark = 1;
- NotifRec.nmSIcon = Prefs.notifOption == notifDiamond ? nil :
- GetResource('SICN', smallDfectIconID);
- if (Prefs.notifOption == notifAlert) {
- NotifRec.nmSound = (Handle)-1;
- NotifRec.nmStr = NotifString;
- GetIndString(NotifString, strListID, msg);
- NotifRec.nmResp = (ProcPtr)-1;
- } else {
- NotifRec.nmSound = nil;
- NotifRec.nmStr = nil;
- NotifRec.nmResp = nil;
- };
- NMInstall((QElemPtr)&NotifRec);
- };
- if (!wait) return;
- while (!InForeground) prog_Event();
- }
-
- /*______________________________________________________________________
-
- misc_Update - Process All Pending Update Events.
- _____________________________________________________________________*/
-
-
- void misc_Update (void)
-
- {
- EventRecord event;
-
- while (EventAvail(updateMask, &event)) prog_Event();
- }
-
- /*______________________________________________________________________
-
- CopyINITResource - Copy Disinfectant INIT Resource.
-
- Entry: oldType = old resource type.
- oldID = old resource id.
- newType = new resource type.
- newID = new resource id.
- newAttr = new resource attributes.
- _____________________________________________________________________*/
-
-
- static OSErr CopyINITResource (OSType oldType, short oldID,
- OSType newType, short newID, short newAttr)
-
- {
- Handle h; /* handle to resource */
- OSErr rCode; /* result code */
-
- h = GetResource(oldType, oldID);
- if (!h) return resNotFound;
- DetachResource(h);
- if (rCode = ResError()) return rCode;
- AddResource(h, newType, newID, "\p");
- if (rCode = ResError()) return rCode;
- SetResAttrs(h, GetResAttrs(h) | newAttr);
- return rCode;
- }
-
- /*______________________________________________________________________
-
- misc_InstallINIT - Install Disinfectant INIT.
- _____________________________________________________________________*/
-
-
- void misc_InstallINIT (void)
-
- {
- }
-
- /*______________________________________________________________________
-
- misc_ExtractINIT - Extract Disinfectant INIT.
- _____________________________________________________________________*/
-
-
- void misc_ExtractINIT (void)
-
- {
- }
-
- /*______________________________________________________________________
-
- misc_PrintError - Handle Printing Error.
- _____________________________________________________________________*/
-
-
- void misc_PrintError (OSErr rCode)
-
- {
- Str255 rCodeStr; /* result code as a string */
-
- if (rCode && rCode != iPrAbort) {
- if (rCode == fnfErr) {
- utl_StopAlert(noDriverID, nil, 0);
- } else if (rCode == resNotFound) {
- utl_StopAlert(noPrinterID, nil, 0);
- } else if (rCode == dskFulErr) {
- utl_StopAlert(printDskFullID, nil, 0);
- } else if (rCode == wPrErr) {
- utl_StopAlert(printLockedID, nil, 0);
- } else {
- NumToString(rCode, rCodeStr);
- ParamText(rCodeStr, nil, nil, nil);
- utl_StopAlert(printErrID, nil, 0);
- };
- };
- }
-
- /*______________________________________________________________________
-
- misc_Periodic - Perform Periodic Tasks.
-
- This routine should be called frequently during disk scans. It
- keeps the beachball cursor spinning. Any other periodic actions
- that might be needed should be added here.
- _____________________________________________________________________*/
-
-
- void misc_Periodic (void)
-
- {
- if (BBEnabled) utl_SpinCursor();
- }
-
-
- /*______________________________________________________________________
-
- misc_DisableBB - Disable Spinning Beachball Cursor.
- _____________________________________________________________________*/
-
- #pragma segment Main
-
- void misc_DisableBB (void)
-
- {
- BBEnabled = false;
- }
-
- #pragma segment misc
-
-
- /*______________________________________________________________________
-
- misc_EnableBB - Enable Spinning Beachball Cursor.
- _____________________________________________________________________*/
-
- void misc_EnableBB (void)
-
- {
- BBEnabled = true;
- }
-
- /*______________________________________________________________________
-
- misc_ListDirectory - List Directory.
-
-
- Entry: pBlock = pointer to PBGetCatInfo param block.
- folderList = handle to folder list.
- refCon = report handle.
- mfs = true if mfs volume.
-
- Exit: function result = true if scan should be canceled.
- _____________________________________________________________________*/
-
-
- Boolean misc_ListDirectory (CInfoPBRec *pBlock, scn_FListElHandle folderList,
- long refCon, Boolean mfs)
-
- {
- Str255 line; /* report line */
- char *p; /* ptr to current position in report line */
-
- #pragma unused (pBlock, refCon, mfs)
-
- /* Check to see if the report is full. */
-
- if (vscn_CheckFull(Report, 3000)) return true;
-
- /* Put three spaces in the report line for each folder level. */
-
- p = line+1;
- folderList = (**folderList).next;
- while (folderList=(**folderList).next) {
- *p++ = ' ';
- *p++ = ' ';
- *p++ = ' ';
- };
-
- /* Copy file or folder name to report line */
-
- memcpy(p, pBlock->hfileInfo.ioNamePtr+1, *pBlock->hfileInfo.ioNamePtr);
- p += *pBlock->hfileInfo.ioNamePtr;
-
- /* Append (ƒ) for folders. */
-
- if (((pBlock->hfileInfo.ioFlAttrib >> 4) & 1) == 1) {
- *p++ = ' ';
- *p++ = '(';
- *p++ = 'ƒ';
- *p++ = ')';
- };
-
- /* Store report line length. */
-
- *line = p - (line + 1);
-
- /* Write the line to the report. */
-
- rep_Append(Report, line, true, true);
- return false;
- }